# !/usr/bin/env python
# -*- encoding: utf-8 -*-

import traceback

import numpy as np
import pandas as pd

# 定义大理工情感词汇情感类别在3维粒度情感特征向量的索引
meaning_dic = {'PA': [2, 9],  # 快乐
               'PE': [2, 10],  # 安心
               'PD': [3, 11],  # 尊敬
               'PH': [3, 12],  # 赞扬
               'PG': [3, 13],  # 相信
               'PB': [3, 14],  # 喜爱
               'PK': [3, 15],  # 祝愿
               'NA': [5, 17],  # 愤怒
               'NB': [6, 18],  # 悲伤
               'NJ': [6, 19],  # 失望
               'NH': [6, 20],  # 疚
               'PF': [6, 21],  # 思
               'NI': [7, 22],  # 慌
               'NC': [7, 23],  # 恐惧
               'NG': [7, 24],  # 羞
               'NE': [8, 25],  # 烦闷
               'ND': [8, 26],  # 憎恶
               'NN': [8, 27],  # 贬责
               'NK': [8, 28],  # 妒忌
               'NL': [8, 29],  # 怀疑
               'PC': [4, 16]}  # 惊奇

# 定义30维情感特征向量每一维的含义
emotion_list = ['positive', 'negative',
                'happy', 'like', 'surprised', 'angry', 'sad', 'fear', 'hate',
                'joyful', 'relieved', 'respect', 'praise', 'believe', 'like', 'hopeful', 'surprised',
                'angry', 'sad', 'disappointed', 'remorse', 'miss', 'fear', 'ashamed', 'flustered',
                'disgusted', 'annoyed', 'reproach', 'jealousy', 'suspect']

origin_df = pd.read_excel('emotion_words.xlsx')
col = ['name']
col.extend(emotion_list)
target_df = pd.DataFrame(columns=col, index=np.arange(origin_df.shape[0]))

err = 0


def func():
    for i in range(origin_df.shape[0]):
        target_df.loc[i][1:] = np.zeros(30)
        target_df.loc[i]['name'] = origin_df.loc[i]['词语']
        try:
            className = 'NA' if type(origin_df.loc[i]['情感分类']) == float else origin_df.loc[i]['情感分类']
            # 处理第一粒度
            if origin_df.loc[i]['极性'] == 1 or origin_df.loc[i]['极性'] == 2:
                target_df.loc[i][origin_df.loc[i]['极性']] += origin_df.loc[i]['强度']
            elif origin_df.loc[i]['极性'] == 3:
                target_df.loc[i][1] += origin_df.loc[i]['强度']
                target_df.loc[i][2] += origin_df.loc[i]['强度']
            # 第二、三粒度
            for index in meaning_dic[className]:
                target_df.loc[i][index + 1] += origin_df.loc[i]['强度']

            # 辅助情感分类
            if type(origin_df.loc[i]['辅助情感分类']) == str:
                className = 'NA' if type(origin_df.loc[i]['辅助情感分类']) == float else origin_df.loc[i]['辅助情感分类']
                # 处理第一粒度
                if origin_df.loc[i]['极性.1'] == 1 or origin_df.loc[i]['极性.1'] == 2:
                    target_df.loc[i][int(origin_df.loc[i]['极性.1'])] += origin_df.loc[i]['强度.1']
                # 第二、三粒度
                for index in meaning_dic[className]:
                    target_df.loc[i][index + 1] += origin_df.loc[i]['强度.1']
        except Exception as e:
            global err
            err += 1
            info = traceback.format_exc()
            print('+++++++++++++出错! 行数：', i, '\n', info)
            print(origin_df.loc[i])


if __name__ == '__main__':
    print(origin_df[:8].head(10))
    print('---------------------------------')
    func()
    target_df.to_csv('emotion_matrix.csv', index=None)
    print(target_df.head(10))
    print('出错总行数：', err)
